/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package gate.learning;

import gate.util.GateException;
import java.util.ArrayList;
import org.jdom.Element;

/**
 *
 * @author asatrya
 */
public class PredictedClassAttribute {
    boolean isClass = false;
    String name;
    int position;
    double weighting;

    public boolean isClass() {
        return isClass;
    }

    public void setClass(boolean isClass) {
        this.isClass = isClass;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getPosition() {
        return position;
    }

    public void setPosition(int position) {
        this.position = position;
    }

    public double getWeighting() {
        return weighting;
    }

    public void setWeighting(double weighting) {
        this.weighting = weighting;
    }

    /**
     * Constructor Create an Attribute object from an xml element.
     */
    public PredictedClassAttribute(Element jdomElement) throws GateException {
        // find the name
        Element anElement = jdomElement.getChild("NAME");
        if (anElement == null) {
            throw new GateException(
                    "Required element \"NAME\" not present in attribute:\n"
                    + jdomElement.toString() + "!");
        } else {
            name = anElement.getTextTrim();
        }
        // find the position if present
        anElement = jdomElement.getChild("POSITION");
        if (anElement == null) {
            position = 0;
        } else {
            position = Integer.parseInt(anElement.getTextTrim());
        }
        // find the weighting if present
        anElement = jdomElement.getChild("WEIGHTING");
        if (anElement == null) {
            weighting = 1.0;
        } else {
            weighting = Double.parseDouble(anElement.getTextTrim());
        }
        //Replace the possible character "_" which was used as separator
        name = name.replaceAll(ConstantParameters.SEPARAPERINFeaturesName,
                ConstantParameters.SEPFeatureNamesREPLACEMENT);
    }

    public PredictedClassAttribute() {
        name = null;
        isClass = false;
        position = 0;
        weighting = 1.0;
    }

    /**
     * This method is a clone of gate.creole.mi.Attribute.parseSerie method with
     * minor changes to make it compatible with ML API. It basically given an
     * attribute element first locates all required variable and creates multiple
     * attributes for the given RANGE.
     */
    public static java.util.List<PredictedClassAttribute> parseSerie(Element jdomElement)
            throws GateException {
        // find the name
        Element anElement = jdomElement.getChild("NAME");
        if (anElement == null) {
            throw new GateException(
                    "Required element \"NAME\" not present in attribute:\n"
                    + jdomElement.toString() + "!");
        }
        String name = anElement.getTextTrim();
        int minpos = 0;
        int maxpos = 0;
        // find the range of this element (e.g. from - to)
        anElement = jdomElement.getChild("RANGE");
        try {
            minpos = Integer.parseInt(anElement.getAttributeValue("from").trim());
            maxpos = Integer.parseInt(anElement.getAttributeValue("to").trim());
        } catch (Exception e) {
            throw new GateException("Range element is uncorrect:\n"
                    + jdomElement.toString() + "!");
        }
        double weighting = 1.0;
        // find the weighting if present
        anElement = jdomElement.getChild("WEIGHTING");
        if (anElement != null) {
            weighting = Double.parseDouble(anElement.getTextTrim());
        }

        //  Replace the possible character "_" which was used as separator
        name = name.replaceAll(ConstantParameters.SEPARAPERINFeaturesName,
                ConstantParameters.SEPFeatureNamesREPLACEMENT);

        // Create a list of Attributes
        ArrayList<PredictedClassAttribute> predictedClassAttributes = new ArrayList<PredictedClassAttribute>();
        for (int position = minpos; position < maxpos + 1; position++) {
            PredictedClassAttribute predictedClassAttribute = new PredictedClassAttribute();
            predictedClassAttribute.setClass(false);
            predictedClassAttribute.setName(name);
            predictedClassAttribute.setPosition(position);
            predictedClassAttribute.setWeighting(weighting);
            predictedClassAttributes.add(predictedClassAttribute);
        }
        return predictedClassAttributes;
    }
}
